//
//  MyListModel.m
//  DA_V
//
//  Created by 中亿世联前台 on 15/5/16.
//  Copyright (c) 2015年 zhouhai. All rights reserved.
//

#import "MyListModel.h"
#import "AFNetworking.h"
#import "InterFace.h"
#import "EncryptionData.h"
#import "ProductModel.h"
#import "CategoryModel.h"
#import "JSONKit.h"
@implementation MyListModel



- (void)setValue:(id)value forUndefinedKey:(NSString *)key{}

#pragma -mark 向服务器获取库存订单已付款的订单
- (void)getCarList:(NSMutableArray *)array parameter:(NSDictionary *)parameter{
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
  
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",nil];

    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:parameter options:NSJSONWritingPrettyPrinted error:nil];
    
    
    NSString *jsonString = [jsonData base64EncodedStringWithOptions:0];
    
//    EncryptionData *enctytion = [[EncryptionData alloc] init];
    
    NSString *str = [self encodeString:jsonString key:PasswordKey];
    
    NSDictionary *paramer = @{@"sign":str};
    
    [manager POST:KuCunListInterface parameters:paramer success:^(AFHTTPRequestOperation *operation, id responseObject) {

       
        
        NSString *str = responseObject[@"sign"];
        
        
        EncryptionData *data = [[EncryptionData alloc]init];
        NSString *datastr = [data decryption:str :PasswordKey];
        
        NSData* dataFromString = [datastr dataUsingEncoding:NSUTF8StringEncoding];
        
        NSDictionary *dataDic = [NSJSONSerialization  JSONObjectWithData:dataFromString options:NSJSONReadingMutableContainers error:nil];
        
        //返回成功
        if ( [[dataDic objectForKey:@"status"] isEqualToString:@"success"]) {
            
            [array removeAllObjects];
            
            NSLog(@"库存订单已付款返回结果 ＝＝＝＝ %@",dataDic);
            NSArray *dataArr = dataDic[@"data"];
            for (int i = 0; i< [dataArr count]; i++) {
                
                NSDictionary *dic = dataArr[i];
                NSMutableDictionary *yifukuanModelDic = [NSMutableDictionary dictionary];
                NSMutableArray *ProductInfoArr = [NSMutableArray array];
                
                MyListModel *myModel = [[MyListModel alloc] init];
                [myModel setValuesForKeysWithDictionary:dic];
                
                [yifukuanModelDic setObject:myModel forKey:@"YiFuKuanModel"];
                
                NSArray *ProductInfoArray = dic[@"ProductInfo"];
                for (int j = 0; j < ProductInfoArray.count; j++) {
                    NSDictionary *productinfoDic = ProductInfoArray[j];
                    MyListModel *mylistModel = [[MyListModel alloc] init];
                    [mylistModel setValuesForKeysWithDictionary:productinfoDic];
                    [ProductInfoArr addObject:mylistModel];
                }
                
                 [yifukuanModelDic setObject:ProductInfoArr forKey:@"ProductInfoModelArr"];
                
                [array addObject:yifukuanModelDic];
                
            }
    
            [self.myListModelDelegate getCuCunlistYiFuKuanEnd];
        } else if([dataDic[@"reason"]hasPrefix:@"暂无"]){
            [array removeAllObjects];
//              [self.myListModelDelegate getCuCunlistYiFuKuanEnd];
            [self.myListModelDelegate getrequesterror:@"暂无相关订单"];
            
//            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"暂无相关订单" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
//            [alert show];
        } else{
//              [self.myListModelDelegate getCuCunlistYiFuKuanEnd];
//            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:dataDic[@"reason"] delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
//            [alert show];
            [self.myListModelDelegate getrequesterror:dataDic[@"reason"]];
        }
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [self.myListModelDelegate requestError];
        NSLog(@"========== %@", [error description]);
//        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"网络异常,无法加载数据" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
//        [alert show];
        
    }];
    
}


#pragma -mark 向服务器上拉加载获取库存订单已付款的订单
- (void)getaddCarList:(NSMutableArray *)array parameter:(NSDictionary *)parameter{
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",nil];
    
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:parameter options:NSJSONWritingPrettyPrinted error:nil];
    
    
    NSString *jsonString = [jsonData base64EncodedStringWithOptions:0];
    
    //    EncryptionData *enctytion = [[EncryptionData alloc] init];
    
    NSString *str = [self encodeString:jsonString key:PasswordKey];
    
    NSDictionary *paramer = @{@"sign":str};
    
    [manager POST:KuCunListInterface parameters:paramer success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        
        
        NSString *str = responseObject[@"sign"];
        
        
        EncryptionData *data = [[EncryptionData alloc]init];
        NSString *datastr = [data decryption:str :PasswordKey];
        
        NSData* dataFromString = [datastr dataUsingEncoding:NSUTF8StringEncoding];
        
        NSDictionary *dataDic = [NSJSONSerialization  JSONObjectWithData:dataFromString options:NSJSONReadingMutableContainers error:nil];
        
        //返回成功
        if ( [[dataDic objectForKey:@"status"] isEqualToString:@"success"]) {
            
            
            NSLog(@"库存订单已付款返回结果 ＝＝＝＝ %@",dataDic);
            NSArray *dataArr = dataDic[@"data"];
            for (int i = 0; i< [dataArr count]; i++) {
                
                NSDictionary *dic = dataArr[i];
                NSMutableDictionary *yifukuanModelDic = [NSMutableDictionary dictionary];
                NSMutableArray *ProductInfoArr = [NSMutableArray array];
                
                MyListModel *myModel = [[MyListModel alloc] init];
                [myModel setValuesForKeysWithDictionary:dic];
                
                [yifukuanModelDic setObject:myModel forKey:@"YiFuKuanModel"];
                
                NSArray *ProductInfoArray = dic[@"ProductInfo"];
                for (int j = 0; j < ProductInfoArray.count; j++) {
                    NSDictionary *productinfoDic = ProductInfoArray[j];
                    MyListModel *mylistModel = [[MyListModel alloc] init];
                    [mylistModel setValuesForKeysWithDictionary:productinfoDic];
                    [ProductInfoArr addObject:mylistModel];
                }
                
                [yifukuanModelDic setObject:ProductInfoArr forKey:@"ProductInfoModelArr"];
                
                [array addObject:yifukuanModelDic];
                
            }
            
            [self.myListModelDelegate getCuCunlistYiFuKuanEnd];
        } else if([dataDic[@"reason"]hasPrefix:@"暂无"]){
            [array removeAllObjects];
            //              [self.myListModelDelegate getCuCunlistYiFuKuanEnd];
            [self.myListModelDelegate getrequesterror:@"暂无相关订单"];
            
            //            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"暂无相关订单" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            //            [alert show];
        } else{
            //              [self.myListModelDelegate getCuCunlistYiFuKuanEnd];
            //            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:dataDic[@"reason"] delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            //            [alert show];
            [self.myListModelDelegate getrequesterror:dataDic[@"reason"]];
        }
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [self.myListModelDelegate requestError];
        NSLog(@"========== %@", [error description]);
        //        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"网络异常,无法加载数据" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        //        [alert show];
        
    }];
    
}
#pragma -mark 向服务器获取客户的订单
- (void)getUserList:(NSMutableArray *)array parameter:(NSDictionary *)parameter interface:(NSString *)interface{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects: @"text/html", nil];
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:parameter options:NSJSONWritingPrettyPrinted error:nil];
    NSString *jsonString = [jsonData base64EncodedStringWithOptions:0];
    NSString *str = [self encodeString:jsonString key:PasswordKey];
    NSDictionary *paramer = @{@"sign":str};
    [manager POST:interface parameters:paramer success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSString *str = responseObject[@"sign"];
        EncryptionData *data = [[EncryptionData alloc]init];
        NSString *datastr = [data decryption:str :PasswordKey];
        NSData* dataFromString = [datastr dataUsingEncoding:NSUTF8StringEncoding];
        NSDictionary *dataDic = [NSJSONSerialization  JSONObjectWithData:dataFromString options:NSJSONReadingMutableContainers error:nil];
//        [self.myListModelDelegate requestError];
        //返回成功
        if ( [[dataDic objectForKey:@"status"] isEqualToString:@"success"]) {
            
            [array removeAllObjects];
            
            NSLog(@"客户订单已付款返回结果 ＝＝＝＝ %@",dataDic);
            NSArray *dataArr = dataDic[@"data"];
            for (int i = 0; i< [dataArr count]; i++) {
                
                NSDictionary *dic = dataArr[i];
                NSMutableDictionary *yifukuanModelDic = [NSMutableDictionary dictionary];
                NSMutableArray *ProductInfoArr = [NSMutableArray array];
                
                
                MyListModel *myModel = [[MyListModel alloc] init];
                [myModel setValuesForKeysWithDictionary:dic];
                
                [yifukuanModelDic setObject:myModel forKey:@"YiFuKuanModel"];
                
                NSArray *ProductInfoArray = dic[@"ProductInfo"];
                for (int j = 0; j < ProductInfoArray.count; j++) {
                    NSDictionary *productinfoDic = ProductInfoArray[j];
                    MyListModel *mylistModel = [[MyListModel alloc] init];
                    [mylistModel setValuesForKeysWithDictionary:productinfoDic];
                    [ProductInfoArr addObject:mylistModel];
                }
                
                [yifukuanModelDic setObject:ProductInfoArr forKey:@"ProductInfoModelArr"];
                
                [array addObject:yifukuanModelDic];
                
            }
            
            [self.myListModelDelegate getUserlistEnd];
        }  else if ( [[dataDic objectForKey:@"status"] isEqualToString:@"failed"]) {
            [array removeAllObjects];
            [self.myListModelDelegate getUserlistEnd];
            if ([[dataDic objectForKey:@"reason"] hasPrefix:@"暂无"]) {
                
//                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"暂无相关订单" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
//                [alert show];
                [self.myListModelDelegate getrequesterror:@"暂无相关订单"];
                
            } else{
//            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:[dataDic objectForKey:@"reason"] delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
//            [alert show];
                [self.myListModelDelegate getrequesterror:[dataDic objectForKey:@"reason"] ];
            }
        } else{
//            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"服务器异常,无法加载数据" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
//            [alert show];
                    [self.myListModelDelegate getrequesterror:@"服务器异常,无法加载数据"];
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
           [self.myListModelDelegate requestError];
        NSLog(@"========== %@", [error description]);
//        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"网络异常,无法加载数据" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
//        [alert show];
        
    }];
    
}

#pragma -mark 向服务器上拉加载获取客户的订单
- (void)getaddUserList:(NSMutableArray *)array parameter:(NSDictionary *)parameter interface:(NSString *)interface{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects: @"text/html", nil];
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:parameter options:NSJSONWritingPrettyPrinted error:nil];
    NSString *jsonString = [jsonData base64EncodedStringWithOptions:0];
    NSString *str = [self encodeString:jsonString key:PasswordKey];
    NSDictionary *paramer = @{@"sign":str};
    [manager POST:interface parameters:paramer success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSString *str = responseObject[@"sign"];
        EncryptionData *data = [[EncryptionData alloc]init];
        NSString *datastr = [data decryption:str :PasswordKey];
        NSData* dataFromString = [datastr dataUsingEncoding:NSUTF8StringEncoding];
        NSDictionary *dataDic = [NSJSONSerialization  JSONObjectWithData:dataFromString options:NSJSONReadingMutableContainers error:nil];
        //        [self.myListModelDelegate requestError];
        //返回成功
        if ( [[dataDic objectForKey:@"status"] isEqualToString:@"success"]) {
            
            
            NSLog(@"客户订单已付款返回结果 ＝＝＝＝ %@",dataDic);
            NSArray *dataArr = dataDic[@"data"];
            for (int i = 0; i< [dataArr count]; i++) {
                
                NSDictionary *dic = dataArr[i];
                NSMutableDictionary *yifukuanModelDic = [NSMutableDictionary dictionary];
                NSMutableArray *ProductInfoArr = [NSMutableArray array];
                
                
                MyListModel *myModel = [[MyListModel alloc] init];
                [myModel setValuesForKeysWithDictionary:dic];
                
                [yifukuanModelDic setObject:myModel forKey:@"YiFuKuanModel"];
                
                NSArray *ProductInfoArray = dic[@"ProductInfo"];
                for (int j = 0; j < ProductInfoArray.count; j++) {
                    NSDictionary *productinfoDic = ProductInfoArray[j];
                    MyListModel *mylistModel = [[MyListModel alloc] init];
                    [mylistModel setValuesForKeysWithDictionary:productinfoDic];
                    [ProductInfoArr addObject:mylistModel];
                }
                
                [yifukuanModelDic setObject:ProductInfoArr forKey:@"ProductInfoModelArr"];
                
                [array addObject:yifukuanModelDic];
                
            }
            
            [self.myListModelDelegate getUserlistEnd];
        }  else if ( [[dataDic objectForKey:@"status"] isEqualToString:@"failed"]) {
            [array removeAllObjects];
            [self.myListModelDelegate getUserlistEnd];
            if ([[dataDic objectForKey:@"reason"] hasPrefix:@"暂无"]) {
                
                //                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"暂无相关订单" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                //                [alert show];
                [self.myListModelDelegate getrequesterror:@"暂无相关订单"];
                
            } else{
                //            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:[dataDic objectForKey:@"reason"] delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                //            [alert show];
                [self.myListModelDelegate getrequesterror:[dataDic objectForKey:@"reason"] ];
            }
        } else{
            //            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"服务器异常,无法加载数据" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            //            [alert show];
            [self.myListModelDelegate getrequesterror:@"服务器异常,无法加载数据"];
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [self.myListModelDelegate requestError];
        NSLog(@"========== %@", [error description]);
        //        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"网络异常,无法加载数据" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        //        [alert show];
        
    }];
    
}

#pragma mark - 加密
-(NSString *)encodeString:(NSString *)data key:(NSString *)key{
    //    NSData* sampleData = [data dataUsingEncoding:NSUTF8StringEncoding];
    //
    //    NSString * base64String = [sampleData base64EncodedStringWithOptions:0];
    NSLog(@"%@",data);
    NSString *result=[[NSMutableString alloc] init];
    NSInteger keyLength = [key length];
    int position = 0;
    int keyChar = 0;
    int strChar = 0;
    for (int i = 0; i < [data length]; i++) {
        position = i % keyLength;
        strChar = [data characterAtIndex:i];
        keyChar =[key characterAtIndex:position];
        int  ch = strChar ^ keyChar;
        char ca = (char) ch;
        result=[result stringByAppendingString:[NSString stringWithFormat:@"%C",ca]];
    }
    
    NSData* sampleDat1a = [result dataUsingEncoding:NSUTF8StringEncoding];
    
    NSString * base64String1 = [sampleDat1a base64EncodedStringWithOptions:0];
    
    return base64String1;
}

@end
